#ifndef _THREAD_H_
#define _THREAD_H_

#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<cstring>
#include<cstdio>
#include<functional>
#include<cstdint>
namespace ThreadModlue
{
    static uint32_t number = 1;//给线程取名
    class Thread
    {
        using func_t = std::function<void()>;
    private:
        static void* routine(void* args)
        {
            Thread *self = static_cast<Thread *>(args);
            self -> Running();//创建成功开始运行,线程
            if(self->_isdeatch)
                self -> detach();
            //pthread_setname_np(self->_tid,self->_name.c_str());
            self -> _func(self->_tid);
            return nullptr;
        }
        void Running()
        {
            std::cout << "线程运行" << std::endl;
            _isrunning = true;
        }
        void detach()
        {
            std::cout << "线程已分离" << std::endl;
            _isdeatch = true;
        }
    public:
        Thread(func_t func)
        :_tid(0),
         _isdeatch(false),
         _isrunning(false),
         _res(nullptr),
         _func(func)
        {
            _name = "thread-" + std::to_string(number++);
        }
        
        bool Start()
        {
            if(_isrunning) return false;
            int n = pthread_create(&_tid,nullptr,routine,this);//创建一个线程
            if(n != 0)
            {
                //创建失败
                std::cout << "pthread_create error" << strerror(n) << std::endl;
                return false;
            }
            else
            {
                //创建成功
                std::cout << "pthread_create success" << std::endl;
            }
            return true;
        }
        void Detach()
        {
            if(!_isrunning)
            {
                std::cout << "线程未运行,不能分离" << std::endl;
                return;
            }
            if(_isdeatch)
            {
                std::cout << "线程已经是分离状态,无需再次分离" << std::endl;
                return;
            }
            int n = pthread_detach(_tid);
            if(n != 0)
            {
                std::cerr << "pthread_datach error: " << strerror(n) << std::endl;
                return;
            }
            else
            {
                std::cout << "pthread_datach success" << std::endl;
                detach();
            }
            return;
        }
        void Stop()
        {
            if(_isrunning)
            {
                int n = pthread_cancel(_tid);
                if(n != 0)
                {
                    std::cerr << "pthread_cancel error: " << strerror(n) << std::endl;
                    return;
                }
                else
                {
                    std::cout << "pthread_cancel success" << std::endl;
                    _isrunning = false;
                    return;
                }
            }
            else
            {
                std::cout << "线程未运行不能取消" << std::endl;
            }
        }

        void Join()
        {
            if(_isdeatch)
            {
                std::cout << "该线程已经分离,不需要在等待" << std::endl;
                return;
            }

            int n = pthread_join(_tid,&_res);
            if(n!=0)
            {
                std::cerr << "pthread_join error: " << strerror(n) << std::endl;
                return;
            }
            else
            {
                std::cout << "phread_join success" << std::endl;
            }
        }
        ~Thread(){}
    private:
        std::string _name;//线程的名字
        pthread_t _tid;//线程
        bool _isdeatch;//线程是否分离,分离:true,未分离:false
        bool _isrunning;//线程的运行状态,运行:true,关闭:false
        void* _res;//线程的返回值
        func_t _func;//线程所执行的方法
    };
}
#endif